<template>
	<view>
		<view v-if="inPorcessOrderId != null" class="order-process" @click="toOrder">您有进行中的订单,点击查看</view>
		
		<map id="map" :longitude="from.longitude" :latitude="from.latitude" :style="contentStyle" scale="15" :enable-traffic="false" :show-location="true" :enable-poi="true" class="map">
			<cover-image class="location" src="../../static/workbench/location.png" @tap="toLocationHandle()"></cover-image>
		</map>
		<view class="panel">
			<view class="from" @tap="chooseLocationHandle('from')">
				<text>{{ from.address }}</text>
			</view>
			<view class="dashed-line"></view>
			<view class="to" @tap="chooseLocationHandle('to')">
				<text>{{ to.address }}</text>
			</view>
			<button class="btn" @tap="toCreateOrderHandle">查看路线</button>
		</view>
	</view>
</template>

<script>
	
import GtPush from '../../lib/gtpush-min.js'
	//地图点选插件
const chooseLocation = requirePlugin('chooseLocation');
let QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');
let qqmapsdk ;

export default {
	data() {
		return {
			//开始位置
			from: {
				address: '输入你的开始位置',
				longitude: 0,
				latitude: 0
			},
			//结束位置
			to: {
				address: '输入你的目的地', 
				longitude: 0,
				latitude: 0
			},
			
			//窗口样式
			contentStyle: '',
			windowHeight: 0,
			//地图对象
			map: null,
			//标记:开始位置点选或者结束位置点选
			chooseFromOrToflag: null,
			inPorcessOrderId:null,
			chooseType:"",
		};
	},
	methods: {
		loadMap(){
      var _this = this;
      uni.getLocation({
        type:'wgs84',
        success: function(res){
          _this.from.latitude = res.latitude;
          _this.from.longitude = res.longitude;
          //逆地址解析
          _this.initStartAddress(_this.from.latitude,_this.from.longitude);
        }
      })
    },
    toLocationHandle(){
      var _this = this;
      _this.loadMap();
      setTimeout(()=>{
        _this.map.moveToLocation({
          latitude: _this.from.latitude,
          longitude: _this.from.longitude,
        })
      },100)
    },
    moveToLocation(){
      var _this = this;
      
    },
    initStartAddress(latitude, longitude) {
      let _this = this;
      //逆地址解析
      qqmapsdk.reverseGeocoder({
        location: {
          latitude: latitude,
          longitude: longitude
        },success(res) {
          _this.from.address = res.result.address;
        }
      })
    },
    chooseLocationHandle(type){
      this.chooseType = type;
      const key = this.Consts.QQMAP_KEY; 
      const referer = 'fccar'; 
      wx.navigateTo({
        url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer
      });
    },
    chooseLocationCallback(location){
      console.log(location)
      if(!location || !location.address)return;
      let _this = this;
      //给开始或结束位置赋值
      if(_this.chooseType == "from"){
        //开始位置点选
        _this.from.address = location.name;
        _this.from.latitude = location.latitude;
        _this.from.longitude = location.longitude;
      }else{
        //结束位置点选
        _this.to.address = location.name;
        _this.to.latitude = location.latitude;
        _this.to.longitude = location.longitude;
      }
    },
    toCreateOrderHandle(){
      var _this = this
      if(_this.from.longitude == 0 || _this.to.longitude == 0){
        uni.showToast({
          title:"请完善位置信息",icon:"error"
        })
        return;
      }
      uni.setStorageSync("from",_this.from);
      uni.setStorageSync("to",_this.to);
      //跳转到创建订单页
      uni.navigateTo({
        url:"/pages/create_order/create_order"
      })
    },
  },
  onLaunch: function() {
    //开启位置更新监听
    wx.startLocationUpdate({
      type:"wgs84",
      success(res){
        console.log("开始位置实时监听...");
      },fail(res) {
        uni.showToast({
          title:"定位失败",icon:"error"
        })
      }
    })
    wx.onLocationChange((res)=>{
      //获取经纬度
      const {latitude,longitude} = res;
      //把坐标位置传给locationChange 函数
      const location = {latitude:latitude,longitude:longitude};
      uni.$emit("locationChange",location)
    });
  },
	onShow: function() {
    this.chooseLocationCallback(chooseLocation.getLocation())
	},
	onHide: function() {
	},
	onLoad: function() {
		let _this = this;
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: _this.Consts.QQMAP_KEY
    });
	  _this.loadMap();
		_this.map = uni.createMapContext("map",this)
	},
	onUnload: function() {
		
	}
};
</script>

<style lang="less">
@import url('workbench.less');
</style>
